package com.bonc.ioc.demo.utils;

import org.apache.http.client.utils.CloneUtils;

import java.util.*;

/**
 * @author Rain Z on 2020/11/16
 * 数据预处理封装
 */
public class PrepDataUtil {

    /**
     * Map 脱敏
     * strings : 期望脱敏的字段名称
     */
    public static void prepDesensMap (Map<String, Object> map, String... strings) {
        DesensUtil desensUtil = new DesensUtil();
        for (String singleStr : strings) {
            map.put(singleStr, desensUtil.transDesensAuto(map.get(singleStr).toString()));
        }
    }

    /**
     * List <Map> 列表结果脱敏
     * strings : 期望脱敏的字段名称
     */
    public static void prepDesensList (List<Map<String, Object>> resultList, String... strings) {
        for (Map<String, Object> singleMap : resultList) {
            prepDesensMap(singleMap, strings);
        }
    }

    /**
     * Map 脱敏 [对称加密]
     */
    public static void prepDesensMapSymEncode (Map<String, Object> map, String encodeEndwith, String... strings) {
        if (encodeEndwith == null) {
            encodeEndwith = "_sym_encode";
        }

        DesensUtil desensUtil = new DesensUtil();
        for (String singleStr : strings) {
            // 存储原值
            String singleValue = map.get(singleStr).toString();
            // put 脱敏结果（覆盖原值）
            map.put(singleStr, desensUtil.transDesensAuto(singleValue));
            // put 加密结果
            map.put(singleStr + encodeEndwith, Base64Util.encode(singleValue));
        }
    }

    /**
     * List <Map> 列表结果脱敏 [对称加密]
     */
    public static void prepDesensListSymEncode (List<Map<String, Object>> resultList, String encodeEndwith, String... strings) {
        if (encodeEndwith == null) {
            encodeEndwith = "_sym_encode";
        }

        for (Map<String, Object> singleMap : resultList) {
            prepDesensMapSymEncode(singleMap, encodeEndwith, strings);
        }
    }

    /**
     * Map 加密 [对称加密]
     */
    public static void prepEncodeMapSymEncode (Map<String, Object> map, String... strings) {
        for (String singleStr : strings) {
            // 覆盖加密
            map.put(singleStr, Base64Util.encode(map.get(singleStr).toString()));
        }
    }

    /**
     * List <Map> 列表结果加密 [对称加密]
     */
    public static void prepEncodeListSymEncode (List<Map<String, Object>> resultList, String... strings) {
        for (Map<String, Object> singleMap : resultList) {
            prepEncodeMapSymEncode(singleMap, strings);
        }
    }

    /**
     * 数据转换 Map
     */
    public static void prepTransMap (Map<String, Object> map, Map<String, Map<Object, Object>> transDict) {

        for (String key : transDict.keySet()) {
            Map<Object, Object> singleDict = transDict.get(key);
            if (map.containsKey(key)) {
                map.put(key, singleDict.get(map.get(key)));
            }
        }

    }

    /**
     * 数据转换 List <Map>
     */
    public static void prepTransList (List<Map<String, Object>> resultList, Map<String, Map<Object, Object>> transDict) {
        for (Map<String, Object> singleMap : resultList) {
            prepTransMap(singleMap, transDict);
        }
    }

    /**
     * Map 反转
     */
    public static Map<Object, Object> prepTurnMap (Map<Object, Object> map) {

        Map<Object, Object> newMap = new HashMap<>();
        for (Object key : map.keySet()) {
            // 原值
            newMap.put(key, map.get(key));
            // 反转
            newMap.put(map.get(key), key);
        }
        return newMap;
    }

}
